package seismosurfer.gui.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;

import javax.help.CSH;
import javax.swing.ButtonGroup;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

import seismosurfer.gui.TimeSliderToolBar;
import seismosurfer.gui.menuitem.DBMenuItem;
import seismosurfer.gui.menuitem.NNMenuItem;
import seismosurfer.gui.menuitem.QuakesWithMacroMenuItem;
import seismosurfer.gui.menuitem.RangeMenuItem;
import seismosurfer.gui.menuitem.SitesMenuItem;
import seismosurfer.gui.menuitem.TopNMenuItem;
import seismosurfer.http.HttpGateway;

import com.bbn.openmap.gui.AbstractOpenMapMenu;
import com.bbn.openmap.util.Debug;

/**
 * This menu lets you choose the data source and
 * includes RangeMenuItem, TopNMenuItem, NNMenuItem,
 * DBMenuItem, QuakesWithMacroMenuItem, SitesMenuItem.
 * Also provides a control for time evolution. 
 *
 */
public class QueryMenu extends AbstractOpenMapMenu implements ActionListener {
    
    private static final long serialVersionUID = -6981728664402461179L;

    public static final String DEBUG = "QueryMenu";

    private String defaultText = "Query";

    private int defaultMnemonic = 'Q';

    public static String SourceProperty = "source";

    private TimeSliderToolBar tb;

    private String source;

    public QueryMenu() {
        super();
        setText(defaultText);
        setMnemonic(defaultMnemonic);

        CSH.setHelpIDString(this, "queryMenu");

        java.util.List sources = HttpGateway.getCatalogSources();

        JRadioButtonMenuItem item;

        ButtonGroup group = new ButtonGroup();

        SourceActionListener sal = new SourceActionListener();

        for (int i = 0; i < sources.size(); i++) {
            item = new JRadioButtonMenuItem((String) sources.get(i));

            if (i == 0) {
                item.setSelected(true);
                setSource((String) sources.get(i));
            }
            group.add(item);
            add(item);

            item.setActionCommand((String) sources.get(i));
            item.addActionListener(sal);
        }

        addSeparator();

        add(new RangeMenuItem());
        add(new TopNMenuItem());
        addSeparator();

        add(new NNMenuItem());
        add(new DBMenuItem());

        addSeparator();

        JMenuItem timeSliderMI = new JMenuItem("Time Evolution");
        timeSliderMI.addActionListener(this);
        add(timeSliderMI);

        addSeparator();
        add(new QuakesWithMacroMenuItem());

        addSeparator();
        add(new SitesMenuItem());
    }

    class SourceActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            setSource(e.getActionCommand());
        }
    }

    public void actionPerformed(ActionEvent ae) {
        tb.setVisible(true);
        if (Debug.debugging(DEBUG)) {
            Debug.output("\nTimeSliderToolBar setVisible(true)!!!\n");
        }
    }

    public void findAndInit(Object someObj) {
        super.findAndInit(someObj);
        if (someObj instanceof TimeSliderToolBar) {
            tb = (TimeSliderToolBar) someObj;
            addPropertyChangeListener(tb.getTimeSlider());
        }

        if (someObj instanceof PropertyChangeListener) {
            addPropertyChangeListener((PropertyChangeListener) someObj);
            if (Debug.debugging(DEBUG)) {
                Debug.output("PropertyChangeListener:"
                        + someObj.getClass().getName());
            }
            setSource(getSource());
        }

    }

    public void findAndUndo(Object someObj) {
        super.findAndUndo(someObj);
        if (someObj instanceof TimeSliderToolBar) {
            tb = null;
        }
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
        firePropertyChange(SourceProperty, null, source);
        if (Debug.debugging(DEBUG)) {
            Debug.output("SourceProperty:" + source);
        }
    }

}
